package com.techthinker.dreamBuilder.Activitites;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.techthinker.dreamBuilder.R;
import com.techthinker.dreamBuilder.Define.Constant;
import com.techthinker.dreamBuilder.Dictionary.DictionaryAdapter;
import com.techthinker.dreamBuilder.Dictionary.DictionaryAdapter.DictionaryViewHolder;
import com.techthinker.dreamBuilder.Entity.Dictionary;
import com.techthinker.dreamBuilder.Utils.HttpHelper;
import com.techthinker.dreamBuilder.Utils.XMLParser;

public class Dic_fromServerActivity extends Activity {
	private List<Dictionary> serverDics;
	private ListView list_dic;
	private DictionaryAdapter dicAdapter;
	private ImageButton ib_selectall;
	private ImageButton ib_download;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dic_fromserver);

		serverDics = new ArrayList<Dictionary>();
		// add a new dictionary for header
		serverDics.add(new Dictionary());
		try {
			serverDics.addAll(new XMLParser().ParseResXML(Constant.SERVER_URL
					+ Constant.SERVER_DIC_FOLDER
					+ Constant.SERVER_RESOURCE_NAME));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// add a new dictionary for bottom
		serverDics.add(new Dictionary());
		dicAdapter = new DictionaryAdapter(this, R.layout.dic_list_item,
				R.id.tv_dic_name, serverDics);
		list_dic = (ListView) findViewById(R.id.lv_serverDic);
		list_dic.setDivider(null);
		list_dic.setFocusable(false);
		list_dic.setTextFilterEnabled(true);
		list_dic.setAdapter(dicAdapter);

		ib_selectall = (ImageButton) findViewById(R.id.ib_dic_selectall);
		// change the CheckBoxes's states
		ib_selectall.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// remember to refresh the views' states
				list_dic.refreshDrawableState();
				int allItem = list_dic.getChildCount() - 2;
				for (int i = 1; i <= allItem; i++) {
					DictionaryViewHolder dvh = (DictionaryViewHolder) list_dic
							.getChildAt(i).getTag();
					dvh.select.setChecked(!dvh.select.isChecked());
				}
			}
		});

		ib_download = (ImageButton) findViewById(R.id.ib_dic_download);
		ib_download.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// remember to refresh the views' states
				list_dic.refreshDrawableState();
				int allItem = list_dic.getChildCount() - 2;

				HttpHelper httpHelper = new HttpHelper();

				for (int i = 1; i <= allItem; i++) {
					DictionaryViewHolder dvh = (DictionaryViewHolder) list_dic
							.getChildAt(i).getTag();
					Dictionary dic = (Dictionary) list_dic.getItemAtPosition(i);
					if (dvh.select.isChecked()) {
						// download the dictionary files
						httpHelper.downloadToSDFile(Constant.SERVER_URL
								+ Constant.SERVER_DIC_FOLDER + dic.getName()
								+ ".xml", dic.getName() + ".xml");
						httpHelper.downloadToSDFile(
								Constant.SERVER_URL + Constant.SERVER_DIC_IMAGE_FOLDER + dic.getName() + ".jpg",
								dic.getName() + ".jpg");
					}
				}
			}
		});
	}
}
